﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Geodatabase;
using System.Data;
using System.Collections;

namespace EAGL.Data
{
    public class DataTableAdapter
    {
        ICursor cursor;
        private Dictionary<short, short> fieldMapping;
         
        /// <summary>
        ///  Maps the fields of the data table to field indexes in FeatureClass (DataTable index, FeatureClass index)
        /// </summary>
        /// <param name="table"></param>
        private void mapFields(DataTable table)
        {
            fieldMapping = new Dictionary<short, short>();
            short c = (short)table.Columns.Count;
            short i = 0;
            
            while (i < c)
            {
                short index = (short)cursor.FindField(table.Columns[i].ColumnName);
                if (index >=0)
                    fieldMapping.Add(i, index);
                i++;
            }
        }
        
        /// <summary>
        /// Fills the data table with records
        /// </summary>
        /// <param name="table">data table to be filled</param>
        /// <returns>the number of inserted records</returns>
        private void fillByCursor(DataTable table)
        {
            IRow row;
            short c = (short)table.Columns.Count;
            short i;
            mapFields(table);
            while ((row = cursor.NextRow()) != null)
            {
                i = 0;
                DataRow r = table.NewRow();
                while (i < c)
                {
                    r[i] = row.get_Value(fieldMapping[i]);
                    i++;
                }
                table.Rows.Add(r);
            }
        }


        private void fillByCursor(DataTable table, int min, int max)
        {
            if (min >= max)
                throw new ArgumentException("Min and Max values are not suitable.");

            IRow row;
            short c = (short)table.Columns.Count;
            short i = 0;
            mapFields(table);
            
            // move to min
            int index = 0;
            while (index < min)
            {
                cursor.NextRow();
                index++;
            }
            

            while ((row = cursor.NextRow()) != null && index <= max)
            {
    
                DataRow r = table.NewRow();
                while (i < c)
                {
                    r[i] = row.get_Value(fieldMapping[i]);
                    i++;
                }
                table.Rows.Add(r);
                index++;
            }
        }

        

        

        #region =========================== PUBLIC ================================

        

        public DataTableAdapter(ICursor cursor)
        {
            this.cursor = cursor;
        }

        /// <summary>
        /// Fills the data table with records from the supplied cursor
        /// </summary>
        /// <param name="table">data table to be filled</param>
        /// <returns>the number of inserted records</returns>
        public void Fill(DataTable table)
        {
           fillByCursor(table);
        }

        public void Fill(DataTable table, int min, int max)
        {
            fillByCursor(table, min, max);
        }

        #endregion
    }
}
